var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 验收表
    contract: [],

    // 实际铺贴面积
    ares: "",
    // 单价
    price: "",
    // 金额
    amount: "",
    // 用胶量
    consumption: "",
    // 厚度
    thickness: "",
    // 浆层厚度照片
    thicknessList: [],
    issuccessMask: false,
    tonzhi: false,
    typeIndex: 0,
    workList: [],
    issuccessMaskes: false,
    issuccessMask: false,
    workobj1: {
      bricklayTotalSize: "",
      bricklayGlueDosage: "",
      superviseCheckProof: "",
      superviseCheckProof: [],
      bricklayPlaceProof: [],
      bricklayPanoramaProof: [],
      bricklaySpecificProof: [],

    },

    workobj2: {
      bricklayTotalSize: "",
      bricklayGlueDosage: "",
      superviseCheckProof: "",
      superviseCheckProof: [],
      bricklayPlaceProof: [],
      bricklayPanoramaProof: [],
      bricklaySpecificProof: [],

    },
    workobj3: {
      bricklayTotalSize: "",
      bricklayGlueDosage: "",
      superviseCheckProof: "",
      superviseCheckProof: [],
      bricklayPlaceProof: [],
      bricklayPanoramaProof: [],
      bricklaySpecificProof: [],

    }

  },

  HidedispatcCarer() {
    this.setData({
      tonzhi: false,
      issuccessMaskes: false,
    })
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。\\
    prevPage.getOrderDetails()
    prevPage.setData({
      todo: ''
    })
    wx.navigateBack({
      delta: 1,
    })
    wx.setStorageSync('istab', true)
  },
  bindtype(e) {
    let typeIndex = e.currentTarget.dataset.type
    this.setData({
      typeIndex: typeIndex
    })
  },
  // 上传附件
  upload(arr) {
    return new Promise((reslove, reject) => {
      if (arr.length == 0) {
        reslove([])
        return
      }
      wx.showLoading({
        title: '上传中...', //提示的内容
        mask: true //显示透明蒙层 防止触摸穿透
      })
      let finishFilesList = []
      arr.forEach((item, index) => {
        wx.uploadFile({
          url: app.globalData.Interfaceurl + 'saveFileUpload',
          filePath: item,
          name: 'file',
          formData: {
            userId: app.globalData.userId,

          },
          header: {
            "content-type": "multipart/form-data"
          },
          success: function (res) {
            finishFilesList.push(JSON.parse(res.data).fileUrl)
            if (index >= arr.length - 1) {
              setTimeout(() => {
                wx.hideLoading();
                reslove(finishFilesList)
              }, 500)
            }
          },
          fail: err => {
            return err
          }
        })
      })
    })
  },

  gounplonecheck() {
    // if (wx.getStorageSync('up') == false) {
    //   wx.setStorageSync('up', true)
    //   this.up = setTimeout(() => {
    //     wx.setStorageSync('up', false)
    //   }, 1000);

    // } else {
    //   wx.showToast({
    //     title: '请不要频繁点击',
    //     duration: 2500, //持续的时间
    //     icon: 'none', //图标
    //   })
    // }

    if (this.data.workList.some(item => item.bricklayPayAmountProof == '')) {
      wx.showToast({
        title: '您有一个结算未上传凭证',
        duration: 2500, //持续的时间
        icon: 'none', //图标
      })
      return
    }
    let proofArr = []
    this.data.workList.forEach(async (item, index) => {
      item.bricklayPayAmountProof = await this.upload(item.bricklayPayAmountProof)
      let obj = {}
      obj.workId = item.id
      obj.bricklayPayAmountProof = item.bricklayPayAmountProof.join()
      proofArr.push(obj)
      console.log(proofArr)
      if (index == this.data.workList.length - 1) {
        this.uploadBricklayPay(proofArr)
      }
    })




  },
  // 38.监理上传瓦工付款凭证
  uploadBricklayPay(proofArr) {
    console.log(proofArr)
    let t = this
    wx.showLoading({
      title: '加载中。。。', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'uploadBricklayPay',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        orderId: t.data.orderId,
        token: t.data.token,
        proofArr: JSON.stringify(proofArr)
      },
      success: res => {
        if (res.data.result == 0) {
          t.setData({
            issuccessMask: true
          })
          wx.hideLoading()
        } else {
          wx.showToast({
            title: res.data.message,
            duration: 2500, //持续的时间
            icon: 'none', //图标
          })
        }








      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },
  showhide(e) {
    let index = e.currentTarget.dataset.index
    let workList = this.data.workList

    workList[index].isshow = !workList[index].isshow
    console.log(workList[index])
    this.setData({
      workList: workList
    })
    console.log(this.data.workList[index])
  },




  Unplonecontract(e) {
    let index = e.currentTarget.dataset.index
    let todo = e.currentTarget.dataset.todo
    let workList = this.data.workList
    workList[index][todo] = e.detail
    this.setData({
      workList: workList
    })

  },


  // 项目详情接口
  getOrderDetails(showmak) {
    let t = this
    wx.showLoading({
      title: '加载中。。。', //提示的内容
      mask: true //显示透明蒙层 防止触摸穿透
    })
    wx.request({
      url: app.globalData.Interfaceurl + 'getOrderDetails',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        // userId: 1569562832576524288,
        orderId: t.data.orderId,
      },
      success: res => {
        wx.hideLoading();
        let workList = res.data.workList
        let zonshu = 0
        workList.forEach(item => {
          zonshu = zonshu + (item.bricklayPayAmount - 0)

          item.bricklayPayAmountProof = []
          if (item.superviseCheckProof != "") {
            item.superviseCheckProof = item.superviseCheckProof.split(",").map(item1 => this.data.Imageurl + item1)
          } else {
            item.superviseCheckProof = []
          }



          if (item.bricklaySlurryThicknessProof != "") {
            item.bricklaySlurryThicknessProof = item.bricklaySlurryThicknessProof.split(",").map(item1 => this.data.Imageurl + item1)
          } else {
            item.bricklaySlurryThicknessProof = []
          }



          if (item.bricklayPlaceProof != "") {
            item.bricklayPlaceProof = item.bricklayPlaceProof.split(",").map(item1 => this.data.Imageurl + item1)
          } else {
            item.bricklayPlaceProof = []
          }


          if (item.bricklayPanoramaProof != "") {
            item.bricklayPanoramaProof = item.bricklayPanoramaProof.split(",").map(item1 => this.data.Imageurl + item1)
          } else {
            item.bricklayPlaceProof = []
          }

          if (item.bricklaySpecificProof != "") {
            item.bricklaySpecificProof = item.bricklaySpecificProof.split(",").map(item1 => this.data.Imageurl + item1)
          } else {
            item.bricklaySpecificProof = []
          }

        });
        this.setData({
          zonshu: zonshu
        })
        let one = workList.filter(item => item.workType == 1)
        let workobj1 = this.data.workobj1

        if (one.length != 0) {
          workobj1.zong = one.reduce((total, currentValue) => total + currentValue.bricklayPayAmount, 0)
          t.setData({
            workobj1: workobj1,
          })
        }

        let two = workList.filter(item => item.workType == 2)
        if (two.length != 0) {
          let workobj2 = this.data.workobj2
          workobj2.zong = two.reduce((total, currentValue) => total + currentValue.bricklayPayAmount, 0)
          t.setData({
            workobj2: workobj2,
          })
        }
        let treen = workList.filter(item => item.workType == 3)
        if (treen.length != 0) {
          let workobj3 = this.data.workobj3
          workobj3.zong = treen.reduce((total, currentValue) => total + currentValue.bricklayPayAmount, 0)
          t.setData({

            workobj3: workobj3
          })
        }





        t.setData({
          workList: workList,
          projectOrder: res.data.projectOrder,

        })
        let zong = t.data.workList.reduce((total, currentValue) => {
          return total + (currentValue.bricklayPayAmount - 0)
        }, 0)

        this.setData({
          zong: zong
        })





      },
      fail: err => {
        wx.showToast({
          title: '请求失败',
          duration: 2500, //持续的时间
          icon: 'none', //图标
        })
      }
    })
  },



  fakeCallback() {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    let date = new Date()
    let year = date.getFullYear()
    let mouth = date.getMonth() + 1
    mouth = mouth >= 10 ? mouth : "0" + mouth
    let d = date.getDate()
    d = d >= 10 ? d : "0" + d
    let nowday = year + "-" + mouth + "-" + d









    this.setData({
      orderId: options.orderId,
      nowday: nowday,
      token: app.token(),
      Imageurl: app.globalData.Imageurl,
      // workList: workList,
    })


    this.getOrderDetails()
    if (options.todo) {
      this.setData({
        todo: options.todo
      })
    }
    if (options.todo == 'previewacceptance') {
      this.getOrderDetails()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.setStorageSync('up', false)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})